package cn.sc.summer.gateway.util;

import cn.hutool.http.HttpRequest;
import cn.sc.summer.constant.warmup.WarmUpConstant;
import cn.sc.summer.token.util.HeaderUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.StopWatch;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * 类名：服务热启动工具类
 *
 * @author a-xin
 * @date 2024/8/26 11:41
 */
@Slf4j
@Component
public class WarmUpUtil {

    /**
     * 对服务进行预热
     *
     * @param address 地址
     */
    public static String warmUp(String address) {
        StopWatch sw = new StopWatch();
        String url = String.format("http://%s" + WarmUpConstant.WARM_UP, address);

        sw.start();
        String result = HttpRequest.get(url)
                .header(HeaderUtil.getFeignHeaderList())
                .execute().body();

        log.info("<- The first request time: {} ms", sw.getTime(TimeUnit.MILLISECONDS));
        return result;
    }

}
